﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Nova_DAL;
using System.Data.Common;
using System.Data;
using Nova_Common;

public partial class Storage_Storage_Info : System.Web.UI.Page
{
    DbCommand cmd = null;
    Nova_DAL.DbHelper access = new DbHelper();
    string strSQL = "";
    DataTable dtone_storage = null;


    protected void Page_Load(object sender, EventArgs e)
    {
        txt_Tray_ID.Text = Request["Tray_id"];
        if (Session["dtone_storage"] == null)
        {
            tempDtone();
        }
    }

    private void tempDtone()
    {
        dtone_storage = new DataTable("dtone_storage");
        dtone_storage.Columns.Add("id", System.Type.GetType("System.Int32"));
        dtone_storage.Columns.Add("ck", System.Type.GetType("System.Boolean"));
        dtone_storage.Columns.Add("Tray_ID", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Pos_ID", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Mat_Code", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Mat_Name", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Sec_Mat_ID", System.Type.GetType("System.String"));
        dtone_storage.Columns.Add("Mat_Num", System.Type.GetType("System.Decimal"));
        dtone_storage.Columns.Add("Num", System.Type.GetType("System.Decimal"));
        Session["dtone_storage"] = dtone_storage;
    }

    private void GridViewAdd(string str)
    {
        if (Session["dtone_storage"] == null)
        {
            tempDtone();
        }
        dtone_storage = (DataTable)Session["dtone_storage"];
        string[] sArray = str.Split(',');



        int iFlag = 0;
        for (int i = 0; i < dtone_storage.Rows.Count; i++)
        {
            if (dtone_storage.Rows[i]["Tray_ID"].ToString() == sArray[1].ToString() && dtone_storage.Rows[i]["Sec_Mat_ID"].ToString() == sArray[5].ToString())
            {
                DataRow drr = dtone_storage.Rows[Convert.ToInt32(i)];
                drr.BeginEdit();
                drr["Num"] = Convert.ToDecimal(drr["Num"].ToString()) + Convert.ToDecimal(sArray[7].ToString());
                drr.EndEdit();
                Session["dtone_storage"] = dtone_storage;
                Session["type_dtone"] = "1";
                iFlag = 1;
            }
        }

        if (iFlag ==0)
        {
            DataRow dr = dtone_storage.NewRow();
            dr["id"] = dtone_storage.Rows.Count + 1;
            dr["ck"] = true;
            dr["Tray_ID"] = sArray[1].ToString();
            dr["Pos_ID"] = sArray[2].ToString();
            dr["Mat_Code"] = sArray[3].ToString();
            dr["Mat_Name"] = sArray[4].ToString();
            dr["Sec_Mat_ID"] = sArray[5].ToString();
            dr["Mat_Num"] = sArray[6].ToString();
            dr["Num"] = sArray[7].ToString(); ;
            dtone_storage.Rows.Add(dr);
            Session["dtone_storage"] = dtone_storage;
            Session["type_dtone"] = "1";
        }
    }

    protected void ibtn_Add_Click(object sender, ImageClickEventArgs e)
    {
        string strPos_ID = "";
        strSQL = "SELECT Pos_ID FROM TRelations WHERE Tray_ID='" + txt_Tray_ID.Text.Trim() + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        DataSet ds = access.ExecuteDataSet(cmd);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            strPos_ID = ds.Tables[0].Rows[0]["Pos_ID"].ToString();
        }

        string strMat_Name = "";
        strSQL = "SELECT Mat_Name FROM TBase_Material WHERE Mat_Code='" + txt_Mat_Code.Text.Trim() + "'";
        cmd = access.GetSqlStringCommond(strSQL);
        ds = access.ExecuteDataSet(cmd);
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            strMat_Name = ds.Tables[0].Rows[0]["Mat_Name"].ToString();
        }
        else
        {
            ShowBox.MessageBox(this.Page, "该物料条码不存在，请先添加物料信息！");
            return;
        }
        string strSec_Mat_ID = getSec_Mat_ID(txt_Mat_Code.Text.Trim());

        strSQL = @"SELECT TBT.Tray_ID,TBI.Mat_Code,TBS.Mat_Num FROM TBase_Storage TBS LEFT JOIN TBase_Interface TBI ON TBI.Sec_Mat_ID=TBS.Sec_Mat_ID
        	                    LEFT JOIN TRel_TrayPosContainer TRT ON TRT.ContainerID= TBS.ContainerID
        	                    LEFT JOIN TBase_TrayPos TBT ON TRT.Relations_ID=TBT.Relations_ID
                                LEFT JOIN TRelations TRP ON TRP.Tray_ID = TBT.Tray_ID 
                                LEFT JOIN TBase_Position TBP ON TRP.Pos_ID = TBP.Pos_ID 
        	                    WHERE TBI.Sec_Mat_ID='" + strSec_Mat_ID + "' AND TBP.Pos_ID!='200000000000'";

        cmd = access.GetSqlStringCommond(strSQL);
        DataSet ds_S = access.ExecuteDataSet(cmd);
        string strMat_Num = "0";
        if (ds_S != null && ds_S.Tables[0].Rows.Count > 0)
        {
            strMat_Num = ds_S.Tables[0].Rows[0]["Mat_Num"].ToString();
        }



        string str = "True," + txt_Tray_ID.Text.Trim() + "," + strPos_ID + "," + txt_Mat_Code.Text.Trim() + "," + strMat_Name + "," + strSec_Mat_ID + "," + strMat_Num + "," + txt_Act_Num.Text.Trim();
        //str = "True,1001,100120561000,33000005, 液化气（天津食堂专用）(非库存物料),1,0,1";
        GridViewAdd(str);
        ibtn_Close_Click(sender, e);
    }
    protected void ibtn_Close_Click(object sender, ImageClickEventArgs e)
    {
        Session["type_dtone"] = "1";
        this.Page.ClientScript.RegisterStartupScript(typeof(string), "Nova1", "window.returnValue=1;top.close();", true);
    }


    /// <summary>
    /// 生成二级编码
    /// </summary>
    /// <param name="i"></param>
    /// <returns></returns>
    protected string getSec_Mat_ID(string strMat_Code)
    {

        HttpCookie WCode = Request.Cookies["WCode"];
        HttpCookie WName = Request.Cookies["WName"];

        cmd = access.GetStoredProcCommond("SP_Get_Sec_Mat_ID");
        access.AddInParameter(cmd, "@Mat_Code", DbType.String, strMat_Code);
        access.AddInParameter(cmd, "@D01", DbType.String, "");
        access.AddInParameter(cmd, "@D02", DbType.String, "");
        access.AddInParameter(cmd, "@D03", DbType.String, "");
        access.AddInParameter(cmd, "@D04", DbType.String, "");
        access.AddInParameter(cmd, "@D05", DbType.String, "");
        access.AddInParameter(cmd, "@D06", DbType.String, "");
        access.AddInParameter(cmd, "@D07", DbType.String, "");
        access.AddInParameter(cmd, "@D08", DbType.String, "");
        access.AddInParameter(cmd, "@D09", DbType.String, "");
        access.AddInParameter(cmd, "@D10", DbType.String, "");
        access.AddInParameter(cmd, "@S01", DbType.String, "");
        access.AddInParameter(cmd, "@S02", DbType.String, "");
        access.AddInParameter(cmd, "@S03", DbType.String, "");
        access.AddInParameter(cmd, "@S04", DbType.String, "");
        access.AddInParameter(cmd, "@S05", DbType.String, "");
        access.AddInParameter(cmd, "@S06", DbType.String, "");
        access.AddInParameter(cmd, "@S07", DbType.String, "");
        access.AddInParameter(cmd, "@S08", DbType.String, "");
        access.AddInParameter(cmd, "@S09", DbType.String, "");
        access.AddInParameter(cmd, "@S10", DbType.String, "");
        access.AddInParameter(cmd, "@S11", DbType.String, "");
        access.AddInParameter(cmd, "@S12", DbType.String, "");
        access.AddInParameter(cmd, "@S13", DbType.String, "");
        access.AddInParameter(cmd, "@S14", DbType.String, "");
        access.AddInParameter(cmd, "@S15", DbType.String, "");
        access.AddInParameter(cmd, "@S16", DbType.String, "");
        access.AddInParameter(cmd, "@S17", DbType.String, "");
        access.AddInParameter(cmd, "@S18", DbType.String, "");
        access.AddInParameter(cmd, "@S19", DbType.String, "");
        access.AddInParameter(cmd, "@S20", DbType.String, "");
        access.AddInParameter(cmd, "@N01", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N02", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N03", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N04", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N05", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N06", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N07", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N08", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N09", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@N10", DbType.Decimal, 0);
        access.AddInParameter(cmd, "@DynamicState01", DbType.Int16, 0);
        access.AddInParameter(cmd, "@DynamicState02", DbType.Int16, 0);
        access.AddInParameter(cmd, "@DynamicState03", DbType.Int16, 0);
        access.AddInParameter(cmd, "@DynamicState04", DbType.Int16, 0);
        access.AddInParameter(cmd, "@DynamicState05", DbType.Int16, 0);
        access.AddInParameter(cmd, "@TS01", DbType.String, "");
        access.AddInParameter(cmd, "@TS02", DbType.String, "");
        access.AddInParameter(cmd, "@TS03", DbType.String, "");
        access.AddInParameter(cmd, "@TS04", DbType.String, "");
        access.AddInParameter(cmd, "@TS05", DbType.String, "");
        access.AddInParameter(cmd, "@ForceNew", DbType.Int16, 1);
        access.AddInParameter(cmd, "@UserName", DbType.String, "");
        access.AddOutParameter(cmd, "@Sec_Mat_ID", DbType.Int32, 20);
        access.AddOutParameter(cmd, "@ResNO", DbType.Int16, 10);
        access.AddOutParameter(cmd, "@ResMsg", DbType.String, 255);
        access.ExecuteNonQuery(cmd);
        int iSecs = Convert.ToInt32(access.GetParameter(cmd, "@Sec_Mat_ID").Value.ToString());
        string strSecs = iSecs.ToString();
        return strSecs;
    }
}
